package com.example.demo.ImgTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 暂不可用
 * 
 * @author FF998
 */
public class ImgTest {


    public static void main(String[] args) {
        // 指定图片文件夹路径
        String imageFolderPath = "D:/upload_files/yanhai/assets/GCD/img";
        // 输出视频路径
        String outputVideoPath = "D:/upload_files/yanhai/assets/GCD/video/abc.mp4";
        // 图片数量
        int imgSum = 150;
        // 视频时长（单位：秒）
        int videoDuration = 30;
        // 计算帧率
        double frameRate = (double) imgSum / videoDuration;

        // 构建FFmpeg的命令行参数
        List<String> command = new ArrayList<>();
        String ffmpegPath = "D:/SOFT/ffmpeg/bin/ffmpeg.exe";
        command.add(ffmpegPath);

        // 获取文件夹内的所有图片文件，并按名称进行排序
        File folder = new File(imageFolderPath);
        File[] imageFiles = folder.listFiles();
        Arrays.sort(imageFiles, Comparator.comparing(File::getName));

        // 添加图片文件路径和帧间隔
        for (File imageFile : imageFiles) {
            command.add("-loop");
            command.add("1");

            command.add("-i");
            command.add(imageFile.getAbsolutePath());

            command.add("-vf");
            command.add("fps=" + frameRate);

        }
        command.add("-c:v");
        command.add("libx264");

        command.add("-pix_fmt");
        command.add("yuv420p");

        command.add(outputVideoPath);
        command.add("-t");
        // 设置视频的时长
        command.add(String.valueOf(videoDuration));

        // 执行命令行
        try {

             ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 创建处理输入流的线程
            Thread inputThread = new Thread(() -> {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });

            // 创建处理错误流的线程
            Thread errorThread = new Thread(() -> {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.err.println(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });

            // 启动线程
            inputThread.start();
            errorThread.start();

            // 等待进程执行完成
            int exitCode = process.waitFor();

            if (exitCode == 0) {
                System.out.println("视频生成成功！");
            } else {
                System.out.println("视频生成失败：Exit code " + exitCode);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
